<?php

namespace App\Http\Controllers\users;

use App\Http\Controllers\Controller;
use App\Models\users\UsersBankModel;
use App\Models\users\UsersSettingModel;
use App\Models\users\UsersWithdrawLogModel;
use App\Service\users\UsersService;
use App\Service\users\WithdrawService;
use Illuminate\Http\Request;
use lxs\api\ApiResponse;
use lxs\tools\Tools;

class UsersController extends Controller
{

    /**
     * 获取用户信息
     */
    public function getUserInfoData(Request $request) {

        $userInfo = UsersService::instance()->getUserInfo($request->uid);

        if ($userInfo) {
            return ApiResponse::apiResponseData(200, '获取用户信息成功', [
                'user_info' => $userInfo,
            ]);
        }
        return ApiResponse::apiResponseData(400, '获取用户信息失败');
    }


    /**
     * 获取用户详细信息
     */
    public function getUserDetailData(Request $request) {

        $userInfo = UsersService::instance()->getUserDetail($request->uid);

        if ($userInfo) {
            $userInfo->withdraw_commission = Tools::Config('user.withdraw_commission');
            return ApiResponse::apiResponseData(200, '获取用户详细信息成功', [
                'user_info' => $userInfo,
            ]);
        }
        return ApiResponse::apiResponseData(400, '获取用户详细信息失败');
    }


    /**
     * 获取用户设置
     */
    public function getUserSettingData(Request $request) {

        $userSetting = UsersSettingModel::where('uid', $request->uid)->first();

        if (!$userSetting) {
            $userSetting = new UsersSettingModel([
                'uid' => $request->uid,
                'create_time' => time(),
            ]);
            $userSetting->save();
        }

        $userBank = UsersBankModel::where('uid', $request->uid)->first();

        return ApiResponse::apiResponseData(200, '获取用户设置成功', [
            'user_setting' => $userSetting,
            'user_bank' => $userBank,
        ]);
    }


    /**
     * 用户开关设置
     */
    public function setUserSwitch(Request $request) {

        $name = $request->input('name');
        $value = $request->input('value');
        $userInfo = UsersService::instance()->getUserInfo($request->uid);

        $userInfo->userSetting->update([$name => $value]);

        return ApiResponse::apiResponseData(200, '设置成功');
    }


    /**
     * 用户设置表单提交
     */
    public function userSettingSubmit(Request $request) {
        $formData = $request->input();
        try {
            $bankBind = UsersBankModel::where('uid', request()->uid)->first();
            if ($bankBind) {
                $bankBind->update($formData);
            } else {
                $formData['uid'] = request()->uid;
                $formData['create_time'] = time();
                $bankBind = new UsersBankModel($formData);
                $bankBind->save();
            }
            return ApiResponse::apiResponseData(200, '绑定成功');
        } catch (\Exception $e) {
            return ApiResponse::apiResponseData(400, '绑定失败');
        }
    }


    /**
     * 获取注销账号页面数据
     */
    public function getCancelAccountData(Request $request) {
        $data = Tools::Config('user');
        $content = $data['CANCEL_ACCOUNT'];

        return ApiResponse::apiResponseData(200, '获取成功', [
            'content' => $content,
        ]);
    }


    /**
     * 确定注销账号提交
     */
    public function cancelAccountSubmit(Request $request) {
        $type = $request->input('type');
        $userInfo = UsersService::instance()->getUserInfo($request->uid);
        if ($type) {
            if ($userInfo->is_cancel == 1) {
                return ApiResponse::apiResponseData(200, '注销成功', [
                    'status' => 0,
                    'tips' => '您的账户已注销过',
                ]);
            }
            $userInfo->update([
                'is_cancel' => 1,
                'cancel_time' => time(),
            ]);
            return ApiResponse::apiResponseData(200, '注销成功', [
                'status' => 1,
                'tips' => '请注意，该账号注销有30天的冷静期(账号锁定期)。除法律法规规定和注销协议约定情形外，我们将在冷静期满后完成注销，在冷静期内登',
            ]);
        } else {
            $userInfo->update([
                'is_cancel' => 0,
            ]);
            return ApiResponse::apiResponseData(200, '取消注销成功', [
                'status' => 0,
                'tips' => '您的账户已取消注销',
            ]);
        }
    }


    /**
     * 选择身份确认
     */
    public function selectIdentitySubmit(Request $request) {

        $is_lawyer = $request->input('is_lawyer');
        $userInfo = UsersService::instance()->getUserInfo($request->uid);
        $userInfo->update([
            'is_lawyer' => $is_lawyer,
        ]);
        return ApiResponse::apiResponseData(200, '确认成功', [
            'is_lawyer' => $is_lawyer,
        ]);
    }


    /**
     * 用户提现表单提交
     */
    public function userWithdrawSubmit(Request $request) {
        $formData = $request->input();
        $uid = $request->uid;
        $result = WithdrawService::instance()->userWithdrawSubmit($uid, $formData);
        if ($result['code'] == 200) {
            return ApiResponse::apiResponseData(200, '提交成功', ['withdraw_id' => $result['data']['withdraw_id']]);
        } else {
            return ApiResponse::apiResponseData(400, $result['message'], $result);
        }
    }


    /**
     * 获取体现提交成功页面数据
     */
    public function getWithdrawSuccessData(Request $request) {
        $withdraw_id = $request->input('withdraw_id');
        $withdrawInfo = UsersWithdrawLogModel::where('id', $withdraw_id)->first();
        if (!$withdrawInfo) {
            return ApiResponse::apiResponseData(400, '提现信息不存在');
        }
        return ApiResponse::apiResponseData(200, '获取成功', [
            'info' => $withdrawInfo,
        ]);
    }

}
